home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevbmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.8 KB  |  218 lines

  1. /* Copyright (C) 1992, 1993, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevbmp.c,v 1.3 2000/09/19 19:00:11 lpd Exp $ */
  20. /* .BMP file format output drivers */
  21. #include "gdevprn.h"
  22. #include "gdevpccm.h"
  23. #include "gdevbmp.h"
  24.  
  25. /* ------ The device descriptors ------ */
  26.  
  27. private dev_proc_print_page(bmp_print_page);
  28. private dev_proc_print_page(bmp_cmyk_print_page);
  29.  
  30. /* Monochrome. */
  31.  
  32. const gx_device_printer gs_bmpmono_device =
  33. prn_device(prn_std_procs, "bmpmono",
  34.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  35.        X_DPI, Y_DPI,
  36.        0, 0, 0, 0,        /* margins */
  37.        1, bmp_print_page);
  38.  
  39. /* 8-bit (SuperVGA-style) grayscale . */
  40. /* (Uses a fixed palette of 256 gray levels.) */
  41.  
  42. private const gx_device_procs bmpgray_procs =
  43. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  44.         gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
  45. const gx_device_printer gs_bmpgray_device =
  46. prn_device(bmpgray_procs, "bmpgray",
  47.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  48.        X_DPI, Y_DPI,
  49.        0, 0, 0, 0,        /* margins */
  50.        8, bmp_print_page);
  51.  
  52. /* 1-bit-per-plane separated CMYK color. */
  53.  
  54. #define bmp_cmyk_procs(p_map_color_rgb, p_map_cmyk_color)\
  55.     gdev_prn_open, NULL, NULL, gdev_prn_output_page, gdev_prn_close,\
  56.     NULL, p_map_color_rgb, NULL, NULL, NULL, NULL, NULL, NULL,\
  57.     gdev_prn_get_params, gdev_prn_put_params,\
  58.     p_map_cmyk_color, NULL, NULL, NULL, gx_page_device_get_page_device
  59.  
  60. private const gx_device_procs bmpsep1_procs = {
  61.     bmp_cmyk_procs(cmyk_1bit_map_color_rgb, cmyk_1bit_map_cmyk_color)
  62. };
  63. const gx_device_printer gs_bmpsep1_device = {
  64.   prn_device_body(gx_device_printer, bmpsep1_procs, "bmpsep1",
  65.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  66.     X_DPI, Y_DPI,
  67.     0,0,0,0,            /* margins */
  68.     4, 4, 1, 1, 2, 2, bmp_cmyk_print_page)
  69. };
  70.  
  71. /* 8-bit-per-plane separated CMYK color. */
  72.  
  73. private const gx_device_procs bmpsep8_procs = {
  74.     bmp_cmyk_procs(cmyk_8bit_map_color_rgb, cmyk_8bit_map_cmyk_color)
  75. };
  76. const gx_device_printer gs_bmpsep8_device = {
  77.   prn_device_body(gx_device_printer, bmpsep8_procs, "bmpsep8",
  78.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  79.     X_DPI, Y_DPI,
  80.     0,0,0,0,            /* margins */
  81.     4, 32, 255, 255, 256, 256, bmp_cmyk_print_page)
  82. };
  83.  
  84. /* 4-bit planar (EGA/VGA-style) color. */
  85.  
  86. private const gx_device_procs bmp16_procs =
  87. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  88.         pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  89. const gx_device_printer gs_bmp16_device =
  90. prn_device(bmp16_procs, "bmp16",
  91.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  92.        X_DPI, Y_DPI,
  93.        0, 0, 0, 0,        /* margins */
  94.        4, bmp_print_page);
  95.  
  96. /* 8-bit (SuperVGA-style) color. */
  97. /* (Uses a fixed palette of 3,3,2 bits.) */
  98.  
  99. private const gx_device_procs bmp256_procs =
  100. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  101.         pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  102. const gx_device_printer gs_bmp256_device =
  103. prn_device(bmp256_procs, "bmp256",
  104.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  105.        X_DPI, Y_DPI,
  106.        0, 0, 0, 0,        /* margins */
  107.        8, bmp_print_page);
  108.  
  109. /* 24-bit color. */
  110.  
  111. private const gx_device_procs bmp16m_procs =
  112. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  113.         bmp_map_16m_rgb_color, bmp_map_16m_color_rgb);
  114. const gx_device_printer gs_bmp16m_device =
  115. prn_device(bmp16m_procs, "bmp16m",
  116.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  117.        X_DPI, Y_DPI,
  118.        0, 0, 0, 0,        /* margins */
  119.        24, bmp_print_page);
  120.  
  121. /* 32-bit CMYK color (outside the BMP specification). */
  122.  
  123. private const gx_device_procs bmp32b_procs = {
  124.     bmp_cmyk_procs(cmyk_8bit_map_color_rgb, gx_default_cmyk_map_cmyk_color)
  125. };
  126. const gx_device_printer gs_bmp32b_device =
  127. prn_device(bmp32b_procs, "bmp32b",
  128.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  129.        X_DPI, Y_DPI,
  130.        0, 0, 0, 0,        /* margins */
  131.        32, bmp_print_page);
  132.  
  133. /* ------ Private definitions ------ */
  134.  
  135. /* Write out a page in BMP format. */
  136. /* This routine is used for all non-separated formats. */
  137. private int
  138. bmp_print_page(gx_device_printer * pdev, FILE * file)
  139. {
  140.     uint raster = gdev_prn_raster(pdev);
  141.     /* BMP scan lines are padded to 32 bits. */
  142.     uint bmp_raster = raster + (-raster & 3);
  143.     byte *row = gs_alloc_bytes(pdev->memory, bmp_raster, "bmp file buffer");
  144.     int y;
  145.     int code;        /* return code */
  146.  
  147.     if (row == 0)        /* can't allocate row buffer */
  148.     return_error(gs_error_VMerror);
  149.  
  150.     /* Write the file header. */
  151.  
  152.     code = write_bmp_header(pdev, file);
  153.     if (code < 0)
  154.     goto done;
  155.  
  156.     /* Write the contents of the image. */
  157.     /* BMP files want the image in bottom-to-top order! */
  158.  
  159.     for (y = pdev->height - 1; y >= 0; y--) {
  160.     gdev_prn_copy_scan_lines(pdev, y, row, raster);
  161.     fwrite((const char *)row, bmp_raster, 1, file);
  162.     }
  163.  
  164. done:
  165.     gs_free_object(pdev->memory, row, "bmp file buffer");
  166.  
  167.     return code;
  168. }
  169.  
  170. /* Write out a page in separated CMYK format. */
  171. /* This routine is used for all formats. */
  172. private int
  173. bmp_cmyk_print_page(gx_device_printer * pdev, FILE * file)
  174. {
  175.     int plane_depth = pdev->color_info.depth / 4;
  176.     uint raster = bitmap_raster(pdev->width * plane_depth);
  177.     /* BMP scan lines are padded to 32 bits. */
  178.     uint bmp_raster = raster + (-raster & 3);
  179.     byte *row = gs_alloc_bytes(pdev->memory, bmp_raster, "bmp file buffer");
  180.     int y;
  181.     int code = 0;        /* return code */
  182.     int plane;
  183.  
  184.     if (row == 0)        /* can't allocate row buffer */
  185.     return_error(gs_error_VMerror);
  186.  
  187.     for (plane = 0; plane <= 3; ++plane) {
  188.     gx_render_plane_t render_plane;
  189.  
  190.     /* Write the page header. */
  191.  
  192.     code = write_bmp_separated_header(pdev, file);
  193.     if (code < 0)
  194.         break;
  195.  
  196.     /* Write the contents of the image. */
  197.     /* BMP files want the image in bottom-to-top order! */
  198.  
  199.     gx_render_plane_init(&render_plane, (gx_device *)pdev, plane);
  200.     for (y = pdev->height - 1; y >= 0; y--) {
  201.         byte *actual_data;
  202.         uint actual_raster;
  203.  
  204.         code = gdev_prn_get_lines(pdev, y, 1, row, bmp_raster,
  205.                       &actual_data, &actual_raster,
  206.                       &render_plane);
  207.         if (code < 0)
  208.         goto done;
  209.         fwrite((const char *)actual_data, bmp_raster, 1, file);
  210.     }
  211.     }
  212.  
  213. done:
  214.     gs_free_object(pdev->memory, row, "bmp file buffer");
  215.  
  216.     return code;
  217. }
  218.